package com.lidroid.xutils.sample.download;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.sample.R;

/**
 * Created by antwei on 2015/8/23.
 */
public abstract class SubRequestCallBack<T> extends RequestCallBack<T> {

    private AlertDialog alertDialog;
    private boolean showProgress;
    private String progressMessage;
    private Context context;

    /**
     *
     * @param showProgress 显示进度条
     * @param progressMessage 进度条信息
     * @param context
     */
    public SubRequestCallBack(boolean showProgress, String progressMessage, Context context) {
        this.showProgress = showProgress;
        this.progressMessage = progressMessage;
        this.context = context;
    }

    public SubRequestCallBack(int rate, boolean showProgress, String progressMessage, Context context) {
        super(rate);
        this.showProgress = showProgress;
        this.progressMessage = progressMessage;
        this.context = context;
    }

    public SubRequestCallBack(Object userTag, boolean showProgress, String progressMessage, Context context) {
        super(userTag);
        this.showProgress = showProgress;
        this.progressMessage = progressMessage;
        this.context = context;
    }

    public SubRequestCallBack(int rate, Object userTag, boolean showProgress, String progressMessage, Context context) {
        super(rate, userTag);
        this.showProgress = showProgress;
        this.progressMessage = progressMessage;
        this.context = context;
    }

    @Override
    public void onStart() {
        super.onStart();
        if(showProgress){
            try {
                this.alertDialog = ProgressDialog.show(context,"",progressMessage, true, true);
                if (null == progressMessage){
                    this.alertDialog.setContentView(R.layout.progress_default);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }

    }

    @Override
    public void onCancelled() {
        super.onCancelled();
    }

    @Override
    public void onLoading(long total, long current, boolean isUploading) {
        super.onLoading(total, current, isUploading);
    }


    @Override
    public void onSuccess(ResponseInfo<T> responseInfo) {
        if (alertDialog != null ) alertDialog.dismiss();

    }

    @Override
    public void onFailure(HttpException e, String s) {

            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }
}
